<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:include="public/page_css::page_css ('修改预期到货明细')"/>

<body>
<div class="layui-fluid larry-wrapper">
    <div class="layui-row layui-col-space30">
        <div class="layui-col-xs24">
            <section class="panel panel-padding">
                <form id="form1" class="layui-form layui-form-pane" action="">
                    <div class="step-content" id="simplewizardinwidget-steps">
                        <div class="step-pane active" id="simplewizardinwidgetstep1">
                            <div>&nbsp;</div>
                            <input type="hidden" name="id">
                            <!--1-->
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">货品编号</label>
                                    <div class="layui-input-inline">
                                        <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="goodsId"
                                                name="hpid" select2 style="width: 193px"></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">货品名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="hpmc" name="hpmc" placeholder="货品名称" autocomplete="off" class="layui-input "
                                               readonly="readonly"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">规格</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="gg" name="gg" placeholder="规格" readonly="readonly" autocomplete="off"
                                               class="layui-input "/>
                                    </div>
                                </div>
                            </div>
                            <!--2-->
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">入库单位</label>
                                    <div class="layui-input-inline">
                                        <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="rkdw"
                                                name="zdwid" select2 style="width: 193px"></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">主单位</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="yqsl" name="yqsl" placeholder="主单位数量" lay-verify="required" disabled="true"
                                               autocomplete="off" class="layui-input "/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">箱单位</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="xdwsl" name="yqxsl" placeholder="箱单位数量" lay-verify="required" disabled="true"
                                               autocomplete="off" class="layui-input " style="width: 193px"/>
                                    </div>
                                </div>
                            </div>
                            <!--3-->
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">货品批号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="hppc" name="hppc" placeholder="货品批号" lay-verify="required" autocomplete="off"
                                               class="layui-input "/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">生产日期</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" name="scrq" id="scrq"
                                               placeholder="yyyy-MM-dd HH:mm:ss" style="width: 193px"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">失效日期</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" name="sxrq" id="sxrq" disabled="true"
                                               placeholder="yyyy-MM-dd HH:mm:ss"/>
                                    </div>
                                </div>
                            </div>
                            <!--4-->
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">体积(cm³)</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="tj" name="tj" placeholder="体积" readonly="readonly" autocomplete="off"
                                               class="layui-input "/>
                                        <span hidden id="tjS"></span>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">净重（kg）</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="jz" name="jz" placeholder="净重" readonly="readonly" autocomplete="off"
                                               class="layui-input "/>
                                        <span hidden id="jzS"></span>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">毛重（kg）</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="zl" name="zl" placeholder="毛重" readonly="readonly" autocomplete="off"
                                               class="layui-input " style="width: 193px"/>
                                        <span hidden id="zlS"></span>
                                    </div>
                                </div>
                            </div>
                            <!--5-->
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">金额（元）</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="qian" name="qian" placeholder="金额" readonly="readonly" autocomplete="off"
                                               class="layui-input "/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">总金额(元)</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="je" name="je" placeholder="总金额" lay-verify="required" th:value="0"
                                               autocomplete="off" class="layui-input "/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">备注</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="bz" name="bz" placeholder="备注"  autocomplete="off"
                                               class="layui-input "/>
                                    </div>
                                </div>
                            </div>
                             <input type="hidden" th:value="${id}" id="kid">
                             <input type="hidden" th:value="${gysid}" id="gysId">
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
</div>
</body>
<div th:include="public/public_js::public_js"></div>

<script th:inline="javascript">
    $(function () {

    })
</script>
<script>
    var hpId;
    var baozhuangId;
    layui.use(['myform', 'zzpUtils', 'form', 'jquery', 'laydate'], function () {
        var form = layui.form,
            zzpUtils = layui.zzpUtils,
            laydate = layui.laydate,
            $ = layui.jquery;
        laydate.render({
            elem: '#scrq'
            , theme: '#20a0ff'
        });
        laydate.render({
            elem: '#sxrq'
            , theme: '#20a0ff'
        })
        var ids = $("#kid").val();
        zzpUtils.action({
            url: "/wmsEstimatedArrivalOrder/getYqMxInfo",
            data: JSON.stringify({id: ids}),
            success: function (data) {
                console.log(data);
                zzpUtils.loadJsonDataToForm(data);
                $('#goodsId').html('<option value="' + data.hpid + '">' + data.hpbh + '</option>').trigger("change");
                $('#rkdw').html('<option value="' + data.zdwid + '">' + data.ms + '</option>').trigger("change");
                $('#qian').val(data.je);
            }, error: function (xhr, textStatus) {
                console.log(textStatus);
                console.log(xhr)
            }
        });
        //select2查询商品 xxx
        $('#goodsId').select2({
            ajax: {
                url: '/zxsswms/wmsPurchaseOrder/getGoods',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        bh: params.term,
                        gysid: $("#gysId").val()
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].goodsId, text: data[i].goodsBh});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择货品编号',
            allowClear: true
        });
        //货品选择获取货品id
        $("#goodsId").on("select2:select", function (e) {
            var id = $(this).val();
            hpId=id;
            $.ajax({
                url: "/zxsswms/wmsEstimatedArrivalOrder/getOneGood",       //请求地址
                type: "POST",            //请求方式
                data: {id: id},    //请求参数
                success: function (data) {
                    $("#hpmc").val(data.goodsName);
                    $("#gg").val(data.gg);
                    $("#sxrq").val(transferCouponValueTime($('#scrq').val(), data.yxq));//失效日期
                    baozhuangId=data.bzid;
                }
            });
            //清空原来入库单位选择的值
            $("#rkdw").select2("val", " ");
            $('#tj').val('');
            $('#jz').val('');
            $('#zl').val('');
            $('#qian').val('');
        });
        //select2查询入库单位
        $('#rkdw').select2({
            ajax: {
                url: '/zxsswms/wmsEstimatedArrivalOrder/getRkdw',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        hpid: hpId
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].id, text: data[i].ms});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择入库单位',
            allowClear: true
        });
        //入库单位触发事件
        $("#rkdw").on("select2:select", function (e) {
            var id = $(this).val();
            //获取包装信息
            var warehouseunit = wareHouseUnit(baozhuangId);
            for(var i=0;i<warehouseunit.length;i++){
                if(warehouseunit[i].bzlx=='0'){
                    rkdwId=warehouseunit[i].id;
                    zname=warehouseunit[i].ms;
                }
                if(warehouseunit[i].id==id){
                    $("#tj").val(warehouseunit[i].tj);
                    $("#jz").val(warehouseunit[i].jz);
                    $("#zl").val(warehouseunit[i].zl);
                    $("#qian").val(warehouseunit[i].je);
                    $("#tjS").val(warehouseunit[i].tj);
                    $("#jzS").val(warehouseunit[i].jz);
                    $("#zlS").val(warehouseunit[i].zl);
                    // 主单位数量
                    UnitNum = warehouseunit[i].zdwsl;
                    if (warehouseunit[i].bzlx == '0') {//主单位

                        $('#yqsl').attr("disabled", false);
                        $('#xdwsl').attr("disabled", true);

                    } else {//箱单位

                        $('#yqsl').attr("disabled", true);
                        $('#xdwsl').attr("disabled", false);
                    }
                }
            }
        });
        // 查询入库单位
        function wareHouseUnit(id) {
            var DATA;
            $.ajax({
                url: '/zxsswms/wmsPurchaseOrder/packDetailList/' + id,
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                async: false,
                success: function (data) {
                    DATA = data;
                }
            })
            return DATA;
        }

        // 监听主单位数量
        $('#yqsl').on('input propertychange', function (data) {
            var value = data.currentTarget.value;
            var len, lenfloat;
            var volumeS = parseFloat($('#jzS').val());//净重
            var suttleS = parseFloat($('#zlS').val());//毛重
            var grossWeightS = parseFloat($('#tjS').val());//体积
            var money = parseFloat($('#qian').val());//金额
            // 如果value不为空
            if (value && value !== '0') {
                len = parseInt(value);
                lenfloat = parseFloat(value);
                if (value.indexOf('-') >= 0) {
                    $('#yqsl').val('');
                } else if ((lenfloat - len) > 0) {
                    $('#yqsl').val(len);
                } else {
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                    $('#je').val(toFixeds(money * len));

                    // 如果主单位数量为1
                    if (UnitNum === 1) {
                        $('#xdwsl').val(len.toString() + '箱0件');
                    } else {
                        var BOX, PIECE;
                        BOX = parseInt(len / UnitNum);
                        PIECE = len % UnitNum;
                        console.log('BOX', UnitNum, BOX, PIECE);
                        $('#xdwsl').val(BOX + '箱' + PIECE + '件');
                    }
                }
            } else {
                $('#jz').val(volumeS);
                $('#zl').val(suttleS);
                $('#tj').val(grossWeightS);
                $('#je').val(0);
                $('#xdwsl').val('0箱0件');
            }
        })
        //箱单位离开事件
        $('#xdwsl').blur(function () {
            var value = $('#xdwsl').val();
            var a = value.indexOf('箱');
            var b = value.indexOf('件');
            var c = value.charAt(value.length - 1);
            if (a > 0 && b > 0 && c == '件') {

            } else {
                layer.msg("必须符合 xx箱xx件 的格式!");
            }
        });
        // 监听箱单位数量
        $('#xdwsl').on('input propertychange', function (data) {
                var value, indbox, indpiece, box, piece, boxs, pieces;
                var volumeS = parseFloat($('#jzS').val());//净重
                var suttleS = parseFloat($('#zlS').val());//毛重
                var grossWeightS = parseFloat($('#tjS').val());//体积
                var money = parseFloat($('#qian').val());
                value = data.currentTarget.value;
                indbox = value.indexOf('箱');
                indpiece = value.indexOf('件');
                if (indbox > 0 && indpiece > 0) {
                    box = value.substring(0, indbox);
                    piece = value.substring(indbox + 1, indpiece);
                    var unitNumber = parseInt(box) * UnitNum + parseInt(piece);
                    var len;
                    if (unitNumber !== 0) {
                        len = unitNumber;
                        $('#je').val(toFixeds(money * len));
                    } else {
                        len = 1;
                        $('#je').val(0);
                    }
                    $('#yqsl').val(unitNumber);
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                }
            }
        )

        //获取当前日期
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }

        function getNowFormatDate1() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + month + strDate;
            return currentdate;
        }

        /**
         * 优惠券有效期
         * startDate:起始日期
         * valueTime：有效天数
         */
        function transferCouponValueTime(startDate, valueTime) {

            var date = new Date(startDate);
            var newDate = new Date(date.getFullYear(), date.getMonth(), Number(date.getDate()) + Number(valueTime));
            var year1 = date.getFullYear();
            var month1 = date.getMonth() + 1;
            var day1 = date.getDate();
            var year2 = newDate.getFullYear();
            var month2 = newDate.getMonth() + 1;
            var day2 = newDate.getDate();
            /* return "<span>"+year1+"</span>-<span>"+month1+"</span>-<span>"+day1+"</span>一<span>"
                 +year2+"</span>-<span>"+month2+"</span>-<span>"+day2+"</span>";*/

            return year2 + "-" + month2 + "-" + day2;
        }

        function toFixeds(data) {
            var str = "";
            if (data) {
                data = data.toString();
                if (data.indexOf('.') < 0) {
                    str = data;
                } else {
                    str = data.substring(0, data.indexOf('.') + 3);
                }
                return parseFloat(str);
            }
        }




        window.submitFunction = function (data) {
            console.log("来提交了");
            console.log(data);
        }
        form.on("submit(form1btn)", function (data) {
            zzpUtils.confirm("是否确定修改", function (index) {
                     zzpUtils.action({
                          url: "/wmsEstimatedArrivalOrder/updateYqMx",
                          data: JSON.stringify(data.field),
                          success: function (data) {
                              setTimeout(function () {
                                  var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                  parent.layer.close(index); //再执行关闭
                                  top.layer.msg(data.title);
                              }, 500);
                          }, error: function (xhr, textStatus) {
                              console.log(textStatus);
                              console.log(xhr)
                          }
                      })
              })
            $("#form1btn").remove();
            return false;
        });
    });

</script>
</html>